﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SaleOnlineSystem.Controls
{
    public partial class CartRight : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataTable dt = BOL.Shopping.ShoppingCart.Instance.dataItems;
                rpt_Cart.DataSource = dt;
                rpt_Cart.DataBind();
            }
        }

        protected void rpt_Cart_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                // Xoa
                string id = e.CommandArgument.ToString();
                BOL.Shopping.ShoppingCart.Instance.RemoveItem(id);
                BOL.Shopping.ShoppingCart.Instance.UpdateDataItems();
                Response.Redirect("/giohang.htm");
            }
            if (e.CommandName == "cong")
            {
                string id = e.CommandArgument.ToString();
                //BOL.Shopping.ShoppingCart.Instance.RemoveItem(id);
                TextBox temp = (TextBox)e.Item.FindControl("txtQuantity");
                int value = Convert.ToInt32(temp.Text);
                value = value + 1;
                temp.Text = value.ToString();
                BOL.Shopping.ShoppingCart.Instance.setQuantity(id, value);
                Response.Redirect("/giohang.htm");
            }
            if (e.CommandName == "tru")
            {
                string id = e.CommandArgument.ToString();
                //  BOL.Shopping.ShoppingCart.Instance.RemoveItem(id);
                TextBox temp = (TextBox)e.Item.FindControl("txtQuantity");
                int value = Convert.ToInt32(temp.Text);
                value = value - 1;
                temp.Text = value.ToString();
                BOL.Shopping.ShoppingCart.Instance.setQuantity(id, value);
                Response.Redirect("/giohang.htm");
            }

        }

        protected void rpt_Cart_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {

                string id = ((Button)e.Item.FindControl("Button1")).CommandArgument.ToString();
                ((TextBox)e.Item.FindControl("txtQuantity")).Text = BOL.Shopping.ShoppingCart.Instance.getQuantity(id).ToString();

            }
        }


    }
}